import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  routes: []
}

const mutations = {
  setRoutes(state, data) {
    state.routes = [...constantRoutes, ...data]
  }
}

const actions = {
  filterRoutes({ commit }, roles) {
    const myAsyncRoutes = asyncRoutes.filter(item => roles.menus.indexOf(item.name) > -1)
    // 释放下面的注释，即可不受权限的控制
    // myAsyncRoutes.push(...asyncRoutes)
    // 最后添加404页面
    myAsyncRoutes.push({ path: '*', redirect: '/404', hidden: true })
    // 调用 mutattion 处理筛选后的动态路由
    commit('setRoutes', myAsyncRoutes)
    return myAsyncRoutes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
